package org.gpp.proj1.particle;

import org.gpp.proj1.util.ModelFactory;

import com.jme.bounding.BoundingSphere;
import com.jme.curve.BezierCurve;
import com.jme.curve.CurveController;
import com.jme.math.Vector3f;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jmex.effects.particles.ParticleGeometry;


public class Missile {
	
	private Node missileNode;
	private BezierCurve curve;
	
	private Spatial explosion;
	
	private CurveController cc;

	public Missile() {
		
		Vector3f[] points = new Vector3f[4];
		points[0] = new Vector3f(0, 0, 0);
		points[1] = new Vector3f(0, -1, 4);
		points[2] = new Vector3f(0, 2, 11);
		points[3] = new Vector3f(0, 0, 15);
		curve = new BezierCurve("Curve", points);

		Spatial missile = ModelFactory.getInstance().getModel("res/models/missile.3ds");
		missile.setLocalScale(0.1f);
		
		ParticleGeometry jet = ParticleCreator.createParticle("res/particle/missileJet.jme");
		jet.setLocalScale(0.01f);
		jet.setModelBound(new BoundingSphere());
		jet.updateModelBound();

		missileNode = new Node("missile");
		missileNode.attachChild(missile);
		missileNode.attachChild(jet);
		missileNode.setLocalTranslation(new Vector3f(points[0]));
		
		cc = new CurveController(curve, missileNode);
		cc.setActive(false);
		missileNode.addController(cc);
		cc.setUpVector(new Vector3f(0, 1, 0));
		cc.setSpeed(1.3f);
		cc.setRepeatType(Controller.RT_CLAMP);
		
		this.explosion = ParticleCreator.createParticle("res/particle/explosion.jme");
	}
	
	public void activate() {
		this.cc.setActive(true);
	}
	
	public void deactivate() {
		this.cc.setActive(false);
	}
	
	public Node getMissileNode() {
		return missileNode;
	}
	
	public void explode() {
		
		this.missileNode.detachAllChildren();
		this.missileNode.attachChild(this.explosion);
	}
}